package wetalk.software.bupt.com.wetalk.view.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

import wetalk.software.bupt.com.wetalk.DB.WeTalkDB;
import wetalk.software.bupt.com.wetalk.R;
import wetalk.software.bupt.com.wetalk.adapter.MessageRecentAdapter;
import wetalk.software.bupt.com.wetalk.model.po.ChatUser;
import wetalk.software.bupt.com.wetalk.model.po.WeTalkRecent;
import wetalk.software.bupt.com.wetalk.util.CommonUtils;
import wetalk.software.bupt.com.wetalk.view.activity.ChatActivity;
import wetalk.software.bupt.com.wetalk.view.activity.GroupChatActivity;
import wetalk.software.bupt.com.wetalk.view.dialog.DialogTips;


/** 最近会话
  * @ClassName: ConversationFragment
  * @Description: TODO
  * @author smile
  * @date 2014-6-7 下午1:01:37
  */
public class Fragment_Msg extends Fragment implements OnItemClickListener,OnItemLongClickListener{
	MessageRecentAdapter adapter;
	ListView listview;
	private String msg;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){

		View view = inflater.inflate(R.layout.fragment_recent, container, false);
		//判断是否有网络，若没有网络将没有网络的布局显示出来
		View netError = view.findViewById(R.id.rl_error_item);
		boolean isNetConnected = CommonUtils.isNetworkAvailable(getContext());
		if(!isNetConnected){
			netError.setVisibility(View.VISIBLE);
		}else{
			netError.setVisibility(View.GONE);
		}
		msg="北京邮电大学软件学院";

		listview = (ListView)view.findViewById(R.id.list);
		listview.setOnItemClickListener(this);
		listview.setOnItemLongClickListener(this);

		adapter = new MessageRecentAdapter(getActivity(),R.layout.item_conversation,WeTalkDB.create(getActivity()).queryRecents());
		Log.i("", "最近会话查询成功");
		listview.setAdapter(adapter);
		return view;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		//传递给chatactivity的时候需要带上user,这个里还需要msg，别的不用
		WeTalkRecent recent = adapter.getItem(position);
		//重置未读消息
		WeTalkDB.create(getActivity()).resetUnread(recent.getTargetid());
		ChatUser user = new ChatUser();
		user.setUserId(recent.getTargetid());
		user.setAvatar(recent.getAvatar());
		user.setNick(recent.getNick());
		user.setUserName(recent.getUserName());

		if(recent.getTargetid().contains("_g")){
			Intent intent = new Intent(getActivity(), GroupChatActivity.class);
			intent.putExtra("user",user);
			intent.putExtra("msg",msg);
			intent.putExtra("groupID",recent.getTargetid());
			intent.putExtra("groupName",recent.getUserName());

			startActivity(intent);
		}else {
			Intent intent = new Intent(getActivity(), ChatActivity.class);
			intent.putExtra("user",user);
			intent.putExtra("msg",msg);

			startActivity(intent);
		}

	}

	//删除会话
	private void deleteRecent(WeTalkRecent recent){
		adapter.remove(recent);
		WeTalkDB.create(getActivity()).deleteRecent(recent.getTargetid());
		Log.i("", "deleteRecent成功");
	}
	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
		WeTalkRecent recent = adapter.getItem(position);
		showDeleteDialog(recent);
		return true;
	}

	public void showDeleteDialog(final WeTalkRecent recent) {
		DialogTips dialog = new DialogTips(getActivity(),recent.getUserName(),"删除会话", "取消",true,true);
		// 设置成功事件
		dialog.SetOnSuccessListener(new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialogInterface, int userId) {
				deleteRecent(recent);
			}
		});
		// 显示确认对话框
		dialog.show();
		dialog = null;
	}

	private boolean hidden;

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		this.hidden = hidden;
		if(!hidden){
			refresh();
		}
	}

	public void refresh(){
		try {
			getActivity().runOnUiThread(new Runnable() {
				public void run() {
					adapter = new MessageRecentAdapter(getActivity(), R.layout.item_conversation, WeTalkDB.create(getActivity()).queryRecents());
					listview.setAdapter(adapter);
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
		Log.i("", "refresh成功 ");
	}

	@Override
	public void onResume() {
		super.onResume();
		if(!hidden){
			refresh();
		}
	}

}
